/*
* Copyright (c) 2008 crisos.org - claudyus84@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*
*
*/ 

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define IOCTL_SD_CLS	 		0x01
#define IOCTL_SD_INIT			0x02
#define IOCTL_SD_TEXT			0x03
#define IOCTL_SD_TEXT2			0x04
#define IOCTL_SD_SCROLL			0x05
#define IOCTL_SD_TEST			0x06
#define IOCTL_SD_IMAGE			0x07

#define DEVNAME "/dev/lcd"
#define CHECK_OPEN { 						\
			if(!fd_dev){				\
				if(ssd1303_open() == -1){	\
					return 1;		\
				}				\
			}					\
		    }

static int fd_dev = 0;

int ssd1303_open(void) {
	fd_dev = open("/dev/lcd", O_RDWR);
	if (fd_dev <= 0) {
		printf("lcd: open error on /dev/lcd\n");
		return -1;
	}
	return 0;	
}

int main(int argc, char **argv) {	
         int text=0 ,img=0 , opt, xpos=0, ypos=0, must_cls=0, make_test=0;
	 char *txt, *img_file;
	 while ((opt = getopt(argc, argv, "pcx:y:ht:i:")) != -1) {
                switch (opt) {
                case 'x':
                   xpos = atoi(optarg);
                   break;
                case 'y':
                   ypos = atoi(optarg);
                   break;
                case 't':
                   txt = optarg;
		   text=1;
                   break;
		case 'i':
                   img_file = optarg;
		   img=1;
                   break;
		case 'c':
		   must_cls=1;
		   break;
		case 'p':
		   make_test=1;
		   break;
		/*other options can be added here*/
		case 'h':
                default: /* ’?’ */
                   fprintf(stderr, "Usage: %s [-c] [-i png_file|-t text] [-x x_position] [-y y_position] [-p]\n",
                           argv[0]);
                   exit(EXIT_FAILURE);
               }
        }

	CHECK_OPEN;
	if(must_cls)
		ioctl(fd_dev, IOCTL_SD_CLS, 0);	//cls
	
	if (make_test)
		ioctl(fd_dev, IOCTL_SD_TEST, 0);
	
	if (text) {
		//ssd1303_text1(xpos, ypos, txt);
		char *p=malloc(strlen(txt)+2);
		p[0]=xpos;
		p[1]=ypos;
		memcpy (&p[2], txt,strlen(txt));
		ioctl(fd_dev, IOCTL_SD_TEXT, p);
		close (fd_dev);
		return 0;
	} else 
	if (img) {
		fprintf(stderr, "not yet implemented..");
//		return 0;
		char cmd[256];		
		snprintf(cmd, 256, "cp %s /tmp/ssd_tmp.png", img_file);
		system ("png2img /tmp/ssd_tmp.png");

		int fd = open("/tmp/ssd_tmp.png.img",O_RDONLY);
		struct stat buf;
		fstat(fd, &buf);

		char data[buf.st_size+5];
		read (fd, &data[5], buf.st_size);
		data[0]= xpos;
		data[1]= ypos;
//		data[2]=cxpos; data[3]=cypos;
		data[4]=0;
		ioctl(fd_dev, IOCTL_SD_IMAGE, data);
		close (fd_dev);
		return 0;
	} 	

	close(fd_dev);
	return 0;
}






